package cn.dnuihyy406.dnuifood_16110100406.controller.getfood;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.dnuihyy406.dnuifood_16110100406.R;
import cn.dnuihyy406.dnuifood_16110100406.bean.ResultBean;
import cn.dnuihyy406.dnuifood_16110100406.controller.RegisterActivity;
import cn.dnuihyy406.dnuifood_16110100406.listener.OnRetrofitListener;
import cn.dnuihyy406.dnuifood_16110100406.model.GetfoodModel;

public class PurchaseActivity extends AppCompatActivity implements OnRetrofitListener<ResultBean>
        ,View.OnClickListener{
    private String food_id1,user_id,price;
    private TextView food_id,sum,num;
    private EditText address,suggesttime;

    @Override
    public void onFailure(String msg) {

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    SharedPreferences sharedPreferences;
    private Button purchase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_purchase);
        initViews();
        initData();
        initEvents();
    }

    private void  initViews(){
        food_id=findViewById(R.id.food_id1);
        sum=findViewById(R.id.sum1);
        address=findViewById(R.id.address1);
        suggesttime=findViewById(R.id.suggesttime1);
        num=findViewById(R.id.num1);
        purchase=findViewById(R.id.purchase1);

    }


    private void  initData(){
        sharedPreferences = getSharedPreferences("hyy", Context.MODE_PRIVATE);
        user_id = sharedPreferences.getString("userid","");
        food_id1=getIntent().getStringExtra("food_id");
        price=getIntent().getStringExtra("price");
        food_id.setText("菜品id："+food_id1);
        num.setText("数量：1");
        sum.setText("总价："+price+"元");
    }

    private void  initEvents(){
        purchase.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if(address.getText().toString().trim().equals("")||suggesttime.getText().toString().trim().equals("")
                ||num.getText().toString().trim().equals("")){
            Toast.makeText(PurchaseActivity.this,"各项不能为空",Toast.LENGTH_SHORT).show();
        }
        else{
            purchase();
        }
    }

    public void  purchase(){
        GetfoodModel getfoodModel=new GetfoodModel();
        getfoodModel.insertOrder(user_id,food_id1,"1"
                ,price,suggesttime.getText().toString(),address.getText().toString(),this);
    }

    @Override
    public void onSuccess(ResultBean object, int flag) {
        if (object.getSuccess().equals("1")) {
            Toast.makeText(PurchaseActivity.this, "购买成功", Toast.LENGTH_SHORT).show();
            finish();
        } else
            Toast.makeText(PurchaseActivity.this, "购买失败", Toast.LENGTH_SHORT).show();
    }
}
